<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/12/29
 * Time: 13:43
 */
class Namesmodel extends CI_Model
{
    var $conn;

    public function __construct()
    {
        parent::__construct();
        $this->conn = $this->load->database('local', TRUE);
    }

    /**
     * 记录查询日志
     * @param $nameInfo
     * @return mixed
     */
    public function addQueryLog($nameInfo){
        $str = $this->conn->insert_string('qiming.generatename_log', $nameInfo);
        $this->conn->query($str);
        // return $this->conn->insert_id();
    }

    /**
     * 分享成功后添加免费查看名字数量
     * @param $openid
     * @return mixed
     */
    public function pushCount($openid){
        $sql = "select havecount from qiming.testmember  WHERE flag = 1 and openid='$openid'";
        $result = $this->conn->query($sql);
        $num = $result->row_array()['havecount'];
        if ($num < 0) {
            $haveCount =0;
        }else{
            $haveCount =$num;
        }
        $updatasql = "UPDATE qiming.testmember SET havecount=$haveCount+50 WHERE openid = '$openid'";

        $this->conn->query($updatasql);
        return $this->conn->affected_rows();
    }

    /**
     * 获取姓氏信息
     * @param $lastname
     * @return mixed
     */
    public function getLastNameInfo($lastname){
        $sql = "SELECT id,lastname,pinyin,bihua,ftbihua,wuxing,rank,sort,fuxing FROM qiming.xingshibase WHERE flag = 1 and lastname = '$lastname'";
        $sql2 = "SELECT id,font,pinyin,bihua,ftbihua,wuxing FROM qiming.fontbase WHERE flag=1 and font='$lastname'";
        $result = $this->conn->query($sql);
        $num = ($result->row_array()['id']);
        if($num>0){
            return $result->result_array();
        }else{
            $result2 = $this->conn->query($sql2);
            return $result2->result_array();
        }
    }

    /**
     * 获取名字信息
     * @param $firstname
     * @return mixed
     */
    public function getFirstNameInfo($firstname){
        $i = strlen($firstname);
        if($i==3){
            $sql = "SELECT id,font,pinyin,bushou,bihua,ftbihua,wuxing,fanti,`desc` FROM qiming.fontbase WHERE font='$firstname'";
        }else{
            $name1=substr($firstname,0,3);
            $name2=substr($firstname,3,3);
            $sql = "SELECT id,font,pinyin,bushou,bihua,ftbihua,wuxing,fanti,`desc` FROM qiming.fontbase WHERE font='$name1' or font='$name2'";
        }
        $query = $this->conn->query($sql);
        return $query->result_array();
    }
    /**
     * 添加用户刷新日志
     */
    public function addUserRefreshLog($lastname,$openid){
        $sql = "INSERT INTO qiming.refreshlog (lastname,openid) VALUES('$lastname','$openid')";
        $this->conn->query($sql);
    }
    /**
     * 更新用户刷新次数
     * @param $openid
     * @return mixed
     */
    public function updateUserRefreshCount($openid){
        $sql = "select havecount from qiming.testmember  WHERE flag = 1 and openid='$openid'";
        $result = $this->conn->query($sql);
        $num = $result->row_array()['havecount'];
        $haveCount = $num - 10;
        if ($haveCount < 0) {
            $haveCount = 0;
        }
        $updatesql = "UPDATE qiming.testmember SET refresh=refresh+1,havecount=$haveCount WHERE flag = 1 and openid='$openid'";
        $this->conn->query($updatesql);
        return $this->conn->affected_rows();
    }

    /**
     * 获取名字组
     * @param $names
     * @param $lackprop
     * @return mixed
     */
    public function getNamesGroup($names,$lackprop){
        $num = $names['double'];
        $sex = $names['sex'];
        $cur = $names['curpage'] * 10;
        if($names['mode']==1){
            $sql = "SELECT id,`name`,isfull FROM qiming.multi_name WHERE flag = 1 and isfull = 1 and num = $num and sex = $sex  and ".$lackprop."= 1 limit $cur,10";
        }else{
            $sql = "SELECT id,`name`,isfull FROM qiming.multi_name WHERE flag = 1 and isfull = 1 and num = $num and sex = $sex  limit $cur,10";
        }
        $query = $this->conn->query($sql);
        return $query->result_array();
    }

    /**
     * 记录点击名字进入详情记录
     * @param $name
     * @param $lastname
     * @param $openid
     * @param $unionid
     * @param $multi_name_id
     * @return mixed
     */
    public function addClickLog($name,$lastname,$openid,$unionid,$multi_name_id){
        $sql = "INSERT INTO qiming.queryname_log(name,lastname,openid,unionid,multi_name_id) VALUES('$name','$lastname','$openid','$unionid',$multi_name_id)";
        $this->conn->query($sql);
        return $this->conn->affected_rows();
    }

    /**
     * 为选定名字的查询次数加1
     * @param $nameid
     * @return mixed
     */
    public function addQueryCount($nameid){
        $sql = "UPDATE qiming.multi_name SET querycount=querycount+1 WHERE id=$nameid";
        $this->conn->query($sql);
        return $this->conn->affected_rows();
    }

    /**
     * 名字测试记录
     * @param $nickname
     * @param $lastname
     * @param $firstname
     * @param $sex
     * @param $brithday
     * @param $openid
     * @param $unionid
     * @param $lflag
     * @param $fflag
     */
    public function add_review_log($nickname,$lastname,$firstname,$sex,$brithday,$openid,$unionid,$lflag,$fflag){
        $sql = "INSERT INTO qiming.review_log (nickname,lastname,firstname,sex,birthday,openid,unionid,lexist,fexist)VALUES('$nickname','$lastname','$firstname',$sex,'$brithday','$openid','$unionid',$lflag,$fflag)";
        $this->conn->query($sql);
        return $this->conn->affected_rows();
    }

    /**
     * 收藏名字
     * @param $savetype
     * @param $openid
     * @param $lastname
     * @param $firstname
     * @return mixed
     */
    public function collectNames($savetype,$openid,$lastname,$firstname,$nameid,$score,$sex,$birthday){
        date_default_timezone_set('PRC');
        $date = date('Y-m-d H:i:s',time());
        $sql = "SELECT id FROM qiming.user_collection WHERE openid = '$openid' and lastname = '$lastname' and firstname = '$firstname'";
        $updatesql = "UPDATE qiming.user_collection SET isdel=1,createtime='$date',score=$score,sex=$sex,birthday='$birthday' WHERE openid = '$openid' and lastname = '$lastname' and firstname = '$firstname'";
        $addsql = "INSERT INTO qiming.user_collection(openid,firstname,lastname,name_id,score,sex,birthday)VALUES('$openid','$firstname','$lastname',$nameid,$score,$sex,'$birthday')";
        $delsql = "UPDATE qiming.user_collection SET isdel=0,createtime='$date',score=$score,sex=$sex,birthday='$birthday' WHERE openid = '$openid' and lastname = '$lastname' and firstname = '$firstname'";
        //收藏
        if($savetype == 1){
            $result = $this->conn->query($sql);
            $num = ($result->num_rows());
            if($num>0){
                $this->conn->query($updatesql);
                return $this->conn->affected_rows();
            }else{
                $this->conn->query($addsql);
                return $this->conn->affected_rows();
            }
        }else{    //取消
            $this->conn->query($delsql);
            return $this->conn->affected_rows();
        }
    }

    /**
     * 添加收藏记录
     * @param $openid
     * @param $firstname
     * @return mixed
     */
    public function addCollectLog($openid,$firstname,$nameid,$type){
        $sql = "select id from qiming.collectlog where openid='$openid' and firstname='$firstname'";
        $updatesql = "UPDATE qiming.collectlog SET num=num+$type where openid='$openid' and firstname='$firstname'";
        $hupdatesql = "UPDATE qiming.collectlog SET num=num+$type,nameid=$nameid where openid='$openid' and firstname='$firstname'";
        $insertsql = "INSERT INTO qiming.collectlog(openid,firstname,nameid)values ('$openid','$firstname',$nameid)";
        $result = $this->conn->query($sql);
        $num = ($result->num_rows());
        if($num>0){
            if($nameid==0){
                $this->conn->query($updatesql);
            }else{
                $this->conn->query($hupdatesql);
            }
            return $this->conn->affected_rows();
        }else{
            $this->conn->query($insertsql);
            return $this->conn->affected_rows();
        }
    }
    /**
     * 获取收藏的名字
     * @param $openid
     * @return mixed
     */
    public function getCollectNames($openid){
        $sql = "SELECT openid,name_id,firstname,lastname,score,sex,birthday,selected FROM qiming.user_collection WHERE openid = '$openid' and isdel = 1 order by createtime desc";
        $query = $this->conn->query($sql);
        return $query->result_array();
    }

    /**
     * 判断是否收藏
     * @param $nameid
     * @param $openid
     * @param $lastname
     * @return mixed
     */
    public function isSave($openid){
        $sql = "select name_id,firstname,lastname from qiming.user_collection WHERE openid='$openid'and isdel=1";
        $query = $this->conn->query($sql);
        return $query->result_array();
    }

    /**
     * 添加投票日志
     * @param $openid
     * @param $lastname
     * @param $firstname
     * @return mixed
     */
    public function addTicketLog($openid,$name){
        $sql = "INSERT INTO qiming.ticketlog(openid,name,babyname)VALUES('$openid','$name','宝贝')";
        $this->conn->query($sql);
        return $this->conn->insert_id();
    }

    /**
     * 添加投票数
     * @param $openid
     * @param $lastname
     * @param $firstname
     * @param $ticketid
     * @return mixed
     */
    public function addTicketNum($fopenid,$lastname,$firstname,$ticketid){
        $sql = "INSERT INTO qiming.ticketnum(fopenid,lastname,firstname,ticketid)VALUES('$fopenid','$lastname','$firstname',$ticketid)";
        $this->conn->query($sql);
        return $this->conn->affected_rows();
    }

    /**
     * 投票
     * @param $openid
     * @param $lastname
     * @param $firstname
     * @return mixed
     */
    public function updateTicketNum($id,$lastname,$firstname,$selected){
        $addsql ="UPDATE qiming.ticketnum SET ticketnum=ticketnum+1 WHERE flag = 1 and ticketid= $id and lastname='$lastname' and firstname='$firstname'";
        $delsql ="UPDATE qiming.ticketnum SET ticketnum=ticketnum-1 WHERE flag = 1 and ticketid= $id and lastname='$lastname' and firstname='$firstname'";
        if($selected==1){
            $this->conn->query($delsql);
        }else{
            $this->conn->query($addsql);
        }

        return $this->conn->affected_rows();
    }

    /**
     * 获取投票记录
     * @param $openid
     * @return mixed
     */
    public function getTicketHistory($openid,$page){
        $cur = ($page-1)*20;
        $sql = "SELECT count(id) as num,sum(ticketnum) as score,ticketid,createtime FROM qiming.ticketnum where flag=1 AND ticketid in (SELECT id FROM qiming.ticketlog where openid ='$openid') GROUP  BY ticketid ORDER BY createtime desc limit $cur,20";
        $query = $this->conn->query($sql);
        return $query->result_array();
    }

    /**
     * 获取投票详情
     * @param $openid
     * @param $createtime
     * @return mixed
     */
    public function getTicketDetail($ticketid){
        $sql = "SELECT fopenid, lastname,firstname,ticketnum,createtime,selected FROM qiming.ticketnum where flag=1 AND ticketid='$ticketid' ";
        $query = $this->conn->query($sql);
        return $query->result_array();
    }

    /**
     * 获取投票名
     * @param $ticketid
     * @return mixed
     */
    public function getUserN($ticketid){
        $sql = "select babyname from qiming.ticketlog WHERE id=$ticketid";
        $query = $this->conn->query($sql);
        return $query->result_array();
    }

    /**
     * 更新投票名
     * @param $babyname
     * @param $ticketid
     * @return mixed
     */
    public  function updateTickLog($babyname,$ticketid){
        $sql ="UPDATE qiming.ticketlog SET babyname='$babyname' WHERE id=$ticketid";
        $query = $this->conn->query($sql);
    }

    /**
     * 添加某个人投了票名字的信息
     * @param $id
     * @param $lastname
     * @param $firstname
     * @param $selected
     * @param $openid
     */
    public function addRealTickLog($id,$lastname,$firstname,$selected,$openid){
        $sql = "select id from qiming.realticklog WHERE  ticketid=$id and topenid = '$openid' and lastname = '$lastname' and firstname = '$firstname'";
        $insertsql = "INSERT INTO qiming.realticklog(topenid,lastname,firstname,selected,ticketid)VALUES('$openid','$lastname','$firstname',$selected,$id)";
        $updatesql = "UPDATE qiming.realticklog SET selected = $selected where ticketid=$id and topenid = '$openid' and lastname = '$lastname' and firstname = '$firstname'";
        $query = $this->conn->query($sql);
        $num = $query->num_rows();
        if($num>0){
            $this->conn->query($updatesql);
        }else{
            $this->conn->query($insertsql);
        }
    }

    /**
     * 获取某个人投了票名字的信息
     * @param $ticketid
     * @param $openid
     * @return mixed
     */
    public function getRealTickLog($ticketid,$openid){
        $sql = "select lastname,firstname from qiming.realticklog where flag = 1 and selected = -1 and ticketid =$ticketid and topenid = '$openid'";
        $query = $this->conn->query($sql);
        return $query->result_array();
    }

    /**
     * 添加分享记录
     * @param $openid
     */
    public function addShareLog($openid){
        $sql = "INSERT INTO qiming.sharelog(openid)VALUES('$openid')";
        $query = $this->conn->query($sql);
    }

    /**
     * 记录名字被测试的次数
     * @param $firstname
     */
    public function addReviewCount($firstname){
        $sql = "select id from qiming.multi_name WHERE name='$firstname'";
        $insertsql = "insert into qiming.multi_name(name,flag)values('$firstname',0)";
        $updatasql = "UPDATE qiming.multi_name SET reviewcount=reviewcount+1 where name='$firstname'";
        $query = $this->conn->query($sql);
        $num = $query->num_rows();
        if($num>0){
            $this->conn->query($updatasql);
        }else{
            $this->conn->query($insertsql);
        }
    }
    public function test(){
        $sql = "SELECT firstname,lastname,sex,birthday,score FROM user_collection WHERE score=0 and isdel=1";
        $query=  $this->conn->query($sql);
        return $query->result_array();
    }

}